package com.ccf.business.etl.model;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.common.utils.constant.Constants;
import com.common.utils.tool.CronUtil;
import com.service.mybatis.pojo.SuperEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import java.time.LocalDateTime;

/**
 * 任务调度表
 *
 * @author zhaoj
 * @date 2021-12-01 15:15:53
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@TableName("wj_task")
@ApiModel(value="wj_task对象", description="任务调度表")
public class Task extends SuperEntity {
    private static final long serialVersionUID=1L;


    @ApiModelProperty(value = "流程设计ID")
    private String flowId;

    @ApiModelProperty(value = "xxlJobId")
    private String xxlJobId;

    @ApiModelProperty(value = "内容，只记录连线和点")
    private String data;

    @ApiModelProperty(value = "任务名称")
    private String name;

    public static final String SCHEDULEMODE_ONCE = "once";
    public static final String SCHEDULEMODE_CYCLE = "cycle";
    @ApiModelProperty(value = "调度方式  once-单次  cycle-周期")
    private String scheduleMode;

    @JSONField(format = "yyyy-MM-dd")
    @ApiModelProperty(value = "开始生效日期")
    private LocalDateTime startDate;

    @JSONField(format = "yyyy-MM-dd")
    @ApiModelProperty(value = "结束生效日期 为空则表示一直有效")
    private LocalDateTime endDate;

    public static final String SCHEDULE_CYCLE_HOUR = "hour";
    public static final String SCHEDULE_CYCLE_DAY = "day";
    public static final String SCHEDULE_CYCLE_WEEK = "week";
    @ApiModelProperty(value = "调度周期 hour-小时  day-天  week-周")
    private String scheduleCycle;

    @ApiModelProperty(value = "开始小时")
    private Integer startHour;

    @ApiModelProperty(value = "开始分钟")
    private Integer startMin;

    @ApiModelProperty(value = "间隔周期（调度周期为hour 或 week是存在）(星期几 1-周天 2-周一 3-周二 4-周三 5-周四 6-周五 7-周六)")
    private Integer intervalValue;

    public static final String STATUS_WAITTING = "waitting";
    public static final String STATUS_RUNNING = "running";
    public static final String STATUS_FINISH = "finish";
    public static final String STATUS_STOPPING = "stopping";
    public static final String STATUS_FAIL = "fail";
    @ApiModelProperty(value = "状态 waitting-等待中 running-运行中 finish-已完成  stopping - 已停止  fail-运行失败")
    private String status;

    @ApiModelProperty(value = "下次计划执行时间")
    private LocalDateTime nextPlanTime;

    @ApiModelProperty(value = "创建人")
    private String creator;

    /**
     * 提供给前端使用
     * @return
     */
    public String getStarDateTime(){
        if(ObjectUtil.isNull(this.startDate)) {
            return "";
        }
        String startDateTime = LocalDateTimeUtil.format(LocalDateTime.of(startDate.getYear(), startDate.getMonthValue(), startDate.getDayOfMonth(), startHour, startMin, 0), DatePattern.NORM_DATETIME_PATTERN);
        return startDateTime;
    }


    /**
     * 提供给前端使用
     * @return
     */
    public String getEndDateTime(){
        if(ObjectUtil.isNull(this.endDate)) {
            return "";
        }
        String endDateTime = LocalDateTimeUtil.format(LocalDateTime.of(endDate.getYear(), endDate.getMonthValue(), endDate.getDayOfMonth(), 23, 59, 59), DatePattern.NORM_DATETIME_PATTERN);
        return endDateTime;
    }

    public String getInterval(){
        String result = "";
        if(scheduleMode.equals(SCHEDULEMODE_CYCLE)) {
            switch (scheduleCycle) {
                case SCHEDULE_CYCLE_HOUR:
                    result = "每" + intervalValue + "小时";
                    break;
                case SCHEDULE_CYCLE_DAY:
                    result = "每天";
                    break;
                case SCHEDULE_CYCLE_WEEK:
                    result = "每" +  getWeekStr(this.intervalValue);
                    break;
            }
        }
        return result;
    }


    public String getWeekStr(int week){
        String weekStr = "";
        switch (week) {
            case 1:
                weekStr = "周日";
                break;
            case 2:
                weekStr = "周一";
                break;
            case 3:
                weekStr = "周二";
                break;
            case 4:
                weekStr = "周三";
                break;
            case 5:
                weekStr = "周四";
                break;
            case 6:
                weekStr = "周五";
                break;
            case 7:
                weekStr = "周六";
                break;
        }
        return weekStr;
    }


    /**
     * 获取Corn表达式
     * @return
     */
    public String getCorn(){
        //单次调度
        if(SCHEDULEMODE_ONCE.equals(this.getScheduleMode())) {
            return CronUtil.createDateTimeCorn();
        //周期调度
        } else if(SCHEDULEMODE_CYCLE.equals(this.getScheduleMode())){
            //按小时调度
            if(SCHEDULE_CYCLE_HOUR.equals(this.getScheduleCycle())){
                return CronUtil.createHourLoopCon(startHour, startMin, intervalValue);
            //按天
            } else if(SCHEDULE_CYCLE_DAY.equals(this.getScheduleCycle())){
                return CronUtil.createDayLoopCon(startHour, startMin);
            //按周
            } else if(SCHEDULE_CYCLE_WEEK.equals(this.getScheduleCycle())){
                return CronUtil.createWeekLoopCon(startHour, startMin, intervalValue);
            }
        }
        return Constants.EMPTY;
    }

    @Override
    public String toString() {
        return "Task{" +
                "flowId='" + flowId + '\'' +
                ", xxlJobId='" + xxlJobId + '\'' +
                ", data='" + data + '\'' +
                ", name='" + name + '\'' +
                ", scheduleMode='" + scheduleMode + '\'' +
                ", startDate=" + startDate +
                ", endDate=" + endDate +
                ", scheduleCycle='" + scheduleCycle + '\'' +
                ", startHour=" + startHour +
                ", startMin=" + startMin +
                ", intervalValue=" + intervalValue +
                ", status='" + status + '\'' +
                ", nextPlanTime=" + nextPlanTime +
                ", creator='" + creator + '\'' +
                '}';
    }
}
